import 'package:dio/dio.dart';
import 'package:rfid/entity/face_search_entity.dart';
import 'package:rfid/http/http_helper.dart';

import '../entity/book_entity.dart';
import '../entity/book_info_entity.dart';
import '../entity/borrow_again_entity.dart';
import '../entity/borrow_entity.dart';
import '../entity/reader_entity.dart';
import '../entity/return_entity.dart';
import '../entity/version_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';

Future sendPlace(list) async {

  var (status,response) = await HttpHelper.instance.post(placeUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''), 'list': list});
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    return null;
  }else{
    return null;
  }
}
Future<BookEntity?> findBook(bookNo) async {

  var (status,response) = await HttpHelper.instance.post(findBookUrl, data: {'more': settingEntity.deviceNo, 'book_no': bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BookEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}

Future<ReaderEntity?> findReader(readerNo, {String category = ''}) async {
  var (status,response) = await HttpHelper.instance.post(findReaderUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'password':'000000','category':category});

  if(status == 200){
    if(response['status'] == 200){
      var data  = ReaderEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}


Future readerApply(readerNo,name,id) async {
  var (status,response) = await HttpHelper.instance.post(readerApplyUrl,
      data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'reader_name':name,'icr_no':id, 'password':'000000','uid':readerNo});

  if(status == 200){
    return true;
  }else{
    toastWarn("${response['error']}");
    return false;
  }

}

Future bookBorrow(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});

  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    // toastWarn("${response['message']}");
    return (400,response['error']);
  }

}

Future bookBorrowCommon(readerNo,bookNo,realReaderNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo,'trans_remark':"原读者号借书: $realReaderNo"});

  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    // toastWarn("${response['message']}");
    return (400,response['error']);
  }

}

Future bookReturn(bookNo) async {
  var (status,response) = await HttpHelper.instance.post(returnUrl, data: {'more': settingEntity.deviceNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = ReturnEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    // toastWarn("${response['message']}");
    return (400,response['error']);
  }
}

Future bookBorrowAgain(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowAgainUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowAgainEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    return (400, response['error']);
  }
}


Future<BookEntity?> searchBook(BookEntity book) async {
  var (status,response) = await HttpHelper.instance.post(searchBookUrl, data: {'isbn': book.isbn, 'limit': 1, 'include cover':true}, options:  Options(headers: {"app_key":appKey}));
  if(status == 200){
    var data  = BookInfoEntity.fromJson(response);
    var bookInfo = data.list;
    if(bookInfo!=null && bookInfo.isNotEmpty){
      var cover = data.list![0].coverBase64;
      if(cover != null){
        var coverList = cover.split(',');
        if(coverList .isNotEmpty && coverList.length>=2){
          book.cover = data.list![0].coverBase64!.split(',')[1];
        }
      }
      book.summary = data.list![0].summary;
    }
    return book;
  }else{
    toastWarn('$response');
    return null;
  }

}




Future faceAdd(userId,userName,img) async {
  var (status,response) = await HttpHelper.instance.post(faceAddUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),'user_id': userId, 'user_name': userName,'image_base64':img});
  if(status == 200){
    return (true,null);
  }else if(status == 500){
    return (false, response);
  }else{
    return (false, response['error']);
  }

}
Future faceSearch(img) async {
  var (status,response) = await HttpHelper.instance.post(faceSearchUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),'image_base64':img});
  if(status == 200){
    var data  = FaceSearchEntity.fromJson(response);
    return (data,null);
  }else if(status == 500){
    return (false, response);
  }else{
    return (null, response['error']);
  }

}

Future<bool> faceQuery(userId) async {
  var (status,response) = await HttpHelper.instance.post(faceQueryUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),"user_id": userId});
  if(status == 200){
    bool isEmpty  = response['empty'];
    return !isEmpty;
  }else{
    toastWarn('${response['error']}');
    return false;
  }

}


Future kdToken() async {
  var (status,response) = await HttpHelper.instance.post(kdTokenUrl, data: {'appid': 'b75833186a37a7975af0dbb255750195', 'appsecret': '0b932941798cfb6b284e8af03cdee7ed'}
      ,options: Options(headers: {'micptoken': kdMicptoken,'content-type':'application/x-www-form-urlencoded'}));
  if(status == 200){
    if(response['code'] == 200){
      kdMicptoken = response['data']['micptoken'];
      return true;
    }else{
      return false;
    }
  }else{
    return false;
  }

}


// Future kdFaceAdd(userId,img) async {
//   var (status,response) = await HttpHelper.instance.oldPost(kdFaceAddUrl, data: {'perCode': userId, 'epId': settingEntity.kdEpId,'faceFileStr':img}
//       ,options: Options(headers: {'micptoken': kdMicptoken,'content-type':'application/x-www-form-urlencoded'}));
//   if(status == 200){
//     if(response['code'] == 200){
//       return (true,null);
//     }else{
//       return (false,response['msg']);
//     }
//   }else{
//     return (false, 'error');
//   }
//
// }
Future kdFaceSearch(img) async {
  var (status,response) = await HttpHelper.instance.post(kdFaceSearchUrl, data: {'photoStr':img}
      ,options: Options(headers: {'micptoken': kdMicptoken,'content-type':'application/x-www-form-urlencoded'}));
  if(status == 200){
    if(response['code'] == 200){
      return (response['data']['perCode'],null);
    }else{
      return (false,response['msg']);
    }
  }else{
    return (false, 'error');
  }
}


Future getVersion() async {
  var (status,response) = await HttpHelper.instance.post(versionUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),"app_id": "rfid"});
  if(status == 200){
    if(response['status'] == 200){
      return VersionEntity.fromJson(response['data']);
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn('${response['error']}');
    return null;
  }
}
